const { getHotSearch, getSearch } = require("../../api/index.js")

// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: "",
    hotSearch: [],
    value: "", 
    goodsData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getHotSearch().then(res => {
      if (res.data.status == 200){
        this.setData({
          hotSearch: res.data.data.result
        })
      }else{
        // 给出提示
        wx.showToast({
          title: res.data.msg,
          icon: "success",
          duration: 2000
        })
      }
    })
  },
  
  // 内容改变
  onChange(e){
    console.log(e.detail)
    this.setData({
      value: e.detail
    })
  },

  // 点击搜索
  onSearchClick(){
    this.http(this.data.value)
  },

  // 内容搜索
  onSearch(){
    this.http(this.data.value)
  },
  clickGetKeywords(e){
    this.http(e.currentTarget.dataset.hotkey)
  },
  pushVC(search){
    wx.navigateTo({
      url: '/pages/goods/goods?search=' + search,
    })
  },
  http(search){
    // 1、直接跳转
    this.pushVC(search)

    /*
    // 2、获取数据之后跳转
    getSearch({search}).then(res => {
      if (res.data.status == 200){
        // 序列化
        let goods = JSON.stringify(res.data.data)
        wx.navigateTo({
          url: '/pages/goods-second/goods-second?goodsData=' + goods,
        })
      }else{
        // 给出提示
        wx.showToast({
          title: res.data.msg,
          icon: "success",
          duration: 2000
        })
      }
    })
    */
  }

})